<template>
    <div>
        <el-divider content-position="left">您具有一定的操作权限</el-divider>
    </div>

    <div>
        <el-divider>
            <span id="spanlittlepart">查询</span>
        </el-divider>
    </div>

    <!-- 查询 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="用户名" style="width: 180px;">
            <el-input v-model="checkformInline.userinfo" placeholder="userinfo"></el-input>
        </el-form-item>
        <el-form-item label="商品名" style="width: 180px;">
            <el-input v-model="checkformInline.commodityinfo" placeholder="commodityinfo"></el-input>
        </el-form-item>
        <el-form-item label="订单号" style="width: 220px;">
            <el-input v-model="checkformInline.ordernum" placeholder="ordernum"></el-input>
        </el-form-item>
        <el-form-item style="width: 200px;">
            <el-button @click="search" type="primary" plain style="width: 200px;">查询</el-button>
        </el-form-item>
    </el-form>

    <el-divider>
        <el-icon><star-filled /></el-icon>
    </el-divider>

    <!-- 展示表格 -->
    <el-table :data="tableData" style="width: 100%" highlight-current-row="true" :scrollbar-always-on="true">
        <el-table-column prop="userid" label="用户id" width="" />
        <el-table-column prop="userinfo1" label="收件人" width="80px" />
        <el-table-column prop="addressinfo" label="地址" width="180px" />
        <el-table-column prop="start_date" :formatter="Formatter" label="开始时间" width="120px" />
        <el-table-column prop="ordernum" label="订单号" width="" />
        <el-table-column prop="state" label="订单状态" width="100px" column-key="date" :filters="[
                { text: '待发货', value: '待发货' },
                { text: '待收货', value: '待收货' },
                { text: '待评价', value: '待评价' },
                { text: '已完成', value: '已完成' },
            ]" :filter-method="filterHandler" />
        <el-table-column prop="commodityid" label="商品id" width="80" />
        <el-table-column prop="commodityinfo" label="商品名" width="190px" />
        <el-table-column prop="comnum" label="商品数" width="80px" />
        <el-table-column prop="comprice" label="单价" width="80px" />
        <el-table-column prop="sumprice" label="总价格" width="80px" />
        <el-table-column fixed="right" label="操作" width="100px">
            <template #default="scope">
                <div v-if="scope.row.state == '待发货'" >
                    <el-button link type="primary" size="small" style="margin-left: 0px;"
                        @click.prevent="handleSendOut(scope.$index, scope.row)">发货</el-button>
                </div>
                <div v-if="scope.row.state != '待发货'">
                    <el-button link type="primary" size="small" style="margin-left: 0px;" disabled>已发货</el-button>
                </div>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination background layout="prev, pager, next" :current-page="page" @update:current-page="handleCurrentChange"
        :total="total" :page-size="size" style="bottom: 0; text-align: center;margin-left: 45%;margin-top: 5px;" />
</template>
<script setup>
import {
    Guide,
    PictureFilled,
    User,
    HomeFilled,
    ChatLineSquare,
    Apple,
    Document,
    List,
    Tools,
    ShoppingCart,
    CollectionTag,
    Finished,
    InfoFilled,
    Cherry,
    Goods,
    Sell,
    ShoppingCartFull,
    Van,
    Sunrise,
    StarFilled,
    EditPen,
    Edit,
    Promotion,
    Grape,
    Pear,
} from '@element-plus/icons-vue';
import dayjs from 'dayjs';
import { ElMessage, ElMessageBox } from 'element-plus';
import axios from 'axios';
const formLabelWidth = '140px';
const filterHandler = (
    value,
    row,
    column
) => {
    const property = column['property']
    return row[property] === value
}

</script>
<style scoped>
.example-pagination-block+.example-pagination-block {
    margin-top: 10px;
}

.example-pagination-block .example-demonstration {
    margin-bottom: 16px;
}
</style>
<script>

export default {
    setup() {

    },
    data() {
        return {
            stateChosen: '待发货/待收货/待评价/已完成',
            stateOptions: [
                {
                    value: '待发货/待收货/待评价/已完成',
                    label: '待发货/待收货/待评价/已完成',
                },
                {
                    value: '待发货',
                    label: '待发货',
                },
                {
                    value: '待收货',
                    label: '待收货',
                },
                {
                    value: '待评价',
                    label: '待评价',
                },
                {
                    value: '已完成',
                    label: '已完成',
                },
            ],
            formInline: {
                orderid: 999,
                userid: 0,
                addressid: 999,
                start_date: '格式如‘2023-5-14’',
                ordernum: 0,
                state: '待发货/待收货/待评价/已完成',
                commodityid: 0,
                comnum: 999,
                comprice: 0,
                sumprice: 0
            },
            checkformInline: {
                orderid: 999,
                userid: 0,
                addressid: 999,
                start_date: '格式如‘2023-5-14’',
                ordernum: '',
                state: '待发货/待收货/待评价/已完成',
                commodityid: 0,
                comnum: 999,
                comprice: 0,
                sumprice: 0,
                commodityinfo: '',
                userinfo: '',
            },
            dialogFormVisible: false,
            dialogaddFormVisible: false,
            showSearch: false,
            tableData: [],
            tableDataStore: [],
            tableimgList: [],
            tableimg: '',
            order: {
                orderid: 999,
                userid: 0,
                addressid: 999,
                start_date: 0,
                ordernum: 0,
                state: '待发货/待收货/待评价/已完成',
                commodityid: 0,
                comnum: 999,
                comprice: 0,
                sumprice: 0
            },
            form: {
                orderid: 999,
                userid: 0,
                addressid: 999,
                start_date: 0,
                ordernum: 0,
                state: '待发货/待收货/待评价/已完成',
                commodityid: 0,
                comnum: 999,
                comprice: 0,
                sumprice: 0
            },
            searchForm: [],
            searchtime: [],
            year: 0,
            month: 0,
            daynum: 0,
            day: '',
            stateChange: 0,
            page: 1, // 当前页码
            total: 0, // 总条数
            size: 10, // 每页的数据条数
            reallyTable: [],
            selData: [],
        }
    },
    methods: {
        init(){
            axios({
            method: 'post',
            url: 'http://localhost:8080/order/allbackend',
            withCredentials: true,
        }).then(res => {
            this.tableData = res.data.data;
            this.tableDataStore = res.data.data;
            this.tableData = res.data.data.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            this.total = res.data.data.length;
            this.selData = res.data.data;
            console.log(res.data.data);
            for (var i = 0; i < this.tableData.length; i++) {
                this.getState(i);
            }
        })
        },
        handleSendOut(index, row) {
            console.log(row.orderid);
            row.state == '待发货' ? this.stateChange = 1 : this.stateChange = 0;
            ElMessageBox.confirm(
                '确认发货吗？',
                'Warning',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
                .then(() => {
                    axios({
                        method: 'post',
                        url: 'http://localhost:8080/order/sendoutorderbackend',
                        withCredentials: true,
                        data: {
                            orderid: row.orderid,
                            state: this.stateChange,
                        },
                    }).then(res => {
                        this.init()
                        // this.tableData = res.data.data;
                        // this.tableDataStore = res.data.data;
                        // this.tableData = res.data.data.slice(
                        //     (this.page - 1) * this.size,
                        //     this.page * this.size
                        // );
                        // this.total = res.data.data.length;
                        // this.selData = res.data.data;
                        // console.log(res.data.data);
                        // for (var i = 0; i < this.tableData.length; i++) {
                        //     this.getState(i);
                        // }
                        ElMessage({
                            type: 'success',
                            message: '发货成功',
                        })
                    })
                })
                .catch(() => {
                    ElMessage({
                        type: 'info',
                        message: '取消发货操作',
                    })
                })
        },
        getState(index) {
            return this.tableData[index].state == 0 ? this.tableData[index].state = '待发货' : (this.tableData[index].state == 1 ? this.tableData[index].state = '待收货' : (this.tableData[index].state == 2 ? this.tableData[index].state = '待评价' : this.tableData[index].state = '已完成'))
        },
        formatDate(date) {
            var y = new Date(date).getFullYear();
            var m = new Date(date).getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            var d = new Date(date).getDate();
            d = d < 10 ? ('0' + d) : d;
            return y + '-' + m + '-' + d;
        },
        Formatter(row, column) {
            if (row.start_date) {
                //console.log(new Date(row.start_date));
                this.day = new Date(row.start_date);
            }
            //console.log(this.formatDate(this.day));
            return this.formatDate(row.start_date);
        },
        backtosimpleformatDate(date) {
            var y = new Date(date).getFullYear();
            this.year = y;
            var m = new Date(date).getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            this.month = m;
            var d = new Date(date).getDate();
            d = d < 10 ? ('0' + d) : d;
            this.daynum = d;
            return y + '-' + m + '-' + d;
        },
        backtosimpleTime(date) {
            //注意这里的参数需要改一下
            this.searchtime = this.checkformInline.start_date.split('-');
            this.backtosimpleformatDate(date);
            if (this.searchtime[0] == this.year && this.searchtime[1] == this.month && this.searchtime[2] == this.daynum) {
                return 'get';
            }
            else {
                return 'fail';
            }
        },
        search() {
            let arr = []
            for (let i = 0; i < this.tableDataStore.length; i++) {
                if (String(this.tableDataStore[i].userinfo).includes(this.checkformInline.userinfo) &&
                    String(this.tableDataStore[i].commodityinfo).includes(this.checkformInline.commodityinfo) &&
                    String(this.tableDataStore[i].ordernum).includes(this.checkformInline.ordernum)) {
                    arr.push(this.tableDataStore[i])
                }
            }
            console.log(arr);
            this.tableData = arr.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            this.selData = arr;
            this.total = arr.length;
        },
        async handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.page = val
            this.tableData = this.selData.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            // this.currentPage = val;
            const data = { 'page': val }
            // await this.onload(data)
        },
    },
    mounted() {
        this.init()
    }
}
</script>
<style scoped>
#sidepart {
    width: 100%;
    position: absolute;
    top: 5px;
    font-family: '华文中宋';
}

#mainpart {
    width: 100%;
    /* position: absolute; */
    top: 20px;
    /* left: 260px; */
    font-family: '华文中宋';
}

#spanpart {
    font-size: 30px;
    font-family: '华文中宋';
}

/* dialog */
.el-button--text {
    margin-right: 15px;
}

.el-select {
    width: 300px;
}

.el-input {
    width: 300px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}

#spanlittlepart {
    font-size: 15px;
    color: cadetblue;
    font-family: '华文中宋';
}
</style>